home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / Apple Game Sprockets / Examples / SoundSprocketTest / TS3Window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-16  |  8.1 KB  |  338 lines  |  [TEXT/CWIE]

  1. /*
  2.  *    File:        TS3Window.c
  3.  *    Author:        Dan Venolia
  4.  *
  5.  *    Copyright © 1996 Apple Computer, Inc.
  6.  */
  7.  
  8. #include <assert.h>
  9.  
  10. #include "TS3Window.h"
  11.  
  12. enum {
  13.     kWindowDataThumbprint = 'wdat'
  14. };
  15.  
  16.  
  17. typedef struct WindowData {
  18.     unsigned long        thumbprint;
  19.     void*                method[kWindowMethod_COUNT];
  20. } WindowData;
  21.  
  22.  
  23. static WindowData** Window_GetData(
  24.     WindowPtr            inWindow);
  25.  
  26. static WindowMethodPtr Window_GetMethod(
  27.     WindowPtr            inWindow,
  28.     WindowMethod        inMethod);
  29.  
  30.  
  31. /* =============================================================================
  32.  *        Window_Init (external)
  33.  *
  34.  *    Initializes our window stuff.
  35.  * ========================================================================== */
  36. void Window_Init(
  37.     void)
  38. {
  39. }
  40.  
  41.  
  42. /* =============================================================================
  43.  *        Window_Exit (external)
  44.  *
  45.  *    Cleans up.
  46.  * ========================================================================== */
  47. void Window_Exit(
  48.     void)
  49. {
  50. }
  51.  
  52.  
  53. /* =============================================================================
  54.  *        Window_GetData (internal)
  55.  *
  56.  *    Returns the WindowData record associated with this window.  Returns NULL
  57.  *    if the window doesn't have a WindowData record associated with it.
  58.  * ========================================================================== */
  59. WindowData** Window_GetData(
  60.     WindowPtr            inWindow)
  61. {
  62.     WindowData**        windowData;
  63.     
  64.     assert(inWindow != NULL);
  65.     
  66.     windowData = (WindowData**) GetWRefCon(inWindow);
  67.     
  68.     if (windowData != NULL && (*windowData)->thumbprint != kWindowDataThumbprint)
  69.     {
  70.         windowData = NULL;
  71.     }
  72.     
  73.     return windowData;
  74. }
  75.  
  76.  
  77. /* =============================================================================
  78.  *        Window_GetMethod (internal)
  79.  *
  80.  *    Returns the function pointer for the given method ID.  The result may be
  81.  *    NULL.
  82.  * ========================================================================== */
  83. WindowMethodPtr Window_GetMethod(
  84.     WindowPtr            inWindow,
  85.     WindowMethod        inMethod)
  86. {
  87.     WindowMethodPtr        result;
  88.     WindowData**        windowData;
  89.     
  90.     assert(inMethod >= kWindowMethod_FIRST);
  91.     assert(inMethod <  kWindowMethod_COUNT);
  92.     
  93.     result = NULL;
  94.     
  95.     windowData = Window_GetData(inWindow);
  96.     if (windowData != NULL)
  97.     {
  98.         result = (*windowData)->method[inMethod];
  99.     }
  100.     
  101.     return result;
  102. }
  103.  
  104.  
  105. /* =============================================================================
  106.  *        Window_New (external)
  107.  *
  108.  *    Makes a new WindowData structure for the window, points the window's refCon
  109.  *    at it, and fills it in using the metahandler.  The metahandler should return
  110.  *    a window method pointer that corresponds to the given method ID, or NULL
  111.  *    if none.
  112.  * ========================================================================== */
  113. void Window_New(
  114.     WindowPtr            inWindow,
  115.     WindowMethodPtr        (*inMetaHandler)(WindowMethod inMethod))
  116. {
  117.     WindowData**        windowData;
  118.     WindowMethod        method;
  119.     
  120.     assert(inWindow != NULL);
  121.     assert(inMetaHandler != NULL);
  122.     
  123.     // Allocate the window data
  124.     windowData = (WindowData**) NewHandle(sizeof(WindowData));
  125.     assert(windowData != NULL);
  126.     
  127.     // Fill it in
  128.     (*windowData)->thumbprint = kWindowDataThumbprint;
  129.     
  130.     for (method = kWindowMethod_FIRST; method < kWindowMethod_COUNT; method++)
  131.     {
  132.         (*windowData)->method[method] = (*inMetaHandler)(method);
  133.     }
  134.     
  135.     // Point the window refcon at it
  136.     SetWRefCon(inWindow, (long) windowData);
  137. }
  138.  
  139.  
  140. /* =============================================================================
  141.  *        Window_Dispose (external)
  142.  *
  143.  *    Disposes of the window's WindowData structure.
  144.  * ========================================================================== */
  145. void Window_Dispose(
  146.     WindowPtr            inWindow)
  147. {
  148.     WindowData**        windowData;
  149.     
  150.     windowData = Window_GetData(inWindow);
  151.     if (windowData != NULL)
  152.     {
  153.         DisposeHandle((Handle) windowData);
  154.         SetWRefCon(inWindow, 0);
  155.     }
  156. }
  157.  
  158.  
  159. /* =============================================================================
  160.  *        Window_IsMine (external)
  161.  *
  162.  *    Returns true if this is window has been set up with this stuff.
  163.  * ========================================================================== */
  164. Boolean Window_IsMine(
  165.     WindowPtr            inWindow)
  166. {
  167.     return Window_GetData(inWindow) != NULL;
  168. }
  169.  
  170.  
  171. /* =============================================================================
  172.  *        Window_GetSleep (external)
  173.  *
  174.  *    Sets *outSleep to the value that should be passed to WaitNextEvent when this
  175.  *    is the front window.
  176.  * ========================================================================== */
  177. void Window_GetSleep(
  178.     WindowPtr            inWindow,
  179.     UInt32*                outSleep)
  180. {
  181.     WindowMethodPtr        getSleepMethod;
  182.     
  183.     assert(outSleep != NULL);
  184.     
  185.     *outSleep = 0xFFFFFFFF;
  186.     
  187.     if (inWindow != NULL)
  188.     {
  189.         getSleepMethod = Window_GetMethod(inWindow, kWindowMethod_GetSleep);
  190.         if (getSleepMethod != NULL)
  191.         {
  192.             (*getSleepMethod)(inWindow, outSleep);
  193.         }
  194.     }
  195. }
  196.  
  197.  
  198. /* =============================================================================
  199.  *        Window_ConsumeEvent (external)
  200.  *
  201.  *    Sets *outConsumed to true if the window consumed the event, or false if not.
  202.  *    Overriding this method allows the window to do idle time things at null-
  203.  *    event times, or dialog things.
  204.  * ========================================================================== */
  205. void Window_ConsumeEvent(
  206.     WindowPtr            inWindow,
  207.     const EventRecord*    inEvent,
  208.     Boolean*            outConsumed)
  209. {
  210.     WindowMethodPtr        consumeEventMethod;
  211.     
  212.     assert(inEvent != NULL);
  213.     assert(outConsumed != NULL);
  214.     
  215.     *outConsumed = false;
  216.     
  217.     if (inWindow != NULL)
  218.     {
  219.         consumeEventMethod = Window_GetMethod(inWindow, kWindowMethod_ConsumeEvent);
  220.         if (consumeEventMethod != NULL)
  221.         {
  222.             (*consumeEventMethod)(inWindow, inEvent, outConsumed);
  223.         }
  224.     }
  225. }
  226.  
  227.  
  228. /* =============================================================================
  229.  *        Window_MouseDown (external)
  230.  *
  231.  *    Processes a mouse-down in the window content area.
  232.  * ========================================================================== */
  233. void Window_MouseDown(
  234.     WindowPtr            inWindow,
  235.     Point                inWhere)
  236. {
  237.     WindowMethodPtr        mouseDownMethod;
  238.     
  239.     if (inWindow != NULL)
  240.     {
  241.         mouseDownMethod = Window_GetMethod(inWindow, kWindowMethod_MouseDown);
  242.         if (mouseDownMethod != NULL)
  243.         {
  244.             (*mouseDownMethod)(inWindow, inWhere);
  245.         }
  246.     }
  247. }
  248.  
  249.  
  250. /* =============================================================================
  251.  *        Window_KeyDown (external)
  252.  *
  253.  *    Processes a key-down or auto-key event.
  254.  * ========================================================================== */
  255. void Window_KeyDown(
  256.     WindowPtr            inWindow,
  257.     char                inChar,
  258.     char                inKeyCap,
  259.     short                inModifiers,
  260.     Boolean                inAutoKey)
  261. {
  262.     WindowMethodPtr        keyDownMethod;
  263.     
  264.     if (inWindow != NULL)
  265.     {
  266.         keyDownMethod = Window_GetMethod(inWindow, kWindowMethod_KeyDown);
  267.         if (keyDownMethod != NULL)
  268.         {
  269.             (*keyDownMethod)(inWindow, inChar, inKeyCap, inModifiers, inAutoKey);
  270.         }
  271.     }
  272. }
  273.  
  274.  
  275. /* =============================================================================
  276.  *        Window_Update (external)
  277.  *
  278.  *    Redraws the contents of the window in response to an update event.
  279.  * ========================================================================== */
  280. void Window_Update(
  281.     WindowPtr            inWindow)
  282. {
  283.     WindowMethodPtr        updateMethod;
  284.     
  285.     if (inWindow != NULL)
  286.     {
  287.         updateMethod = Window_GetMethod(inWindow, kWindowMethod_Update);
  288.         if (updateMethod != NULL)
  289.         {
  290.             (*updateMethod)(inWindow);
  291.         }
  292.     }
  293. }
  294.  
  295.  
  296. /* =============================================================================
  297.  *        Window_Activate (external)
  298.  *
  299.  *    Handles window activation.
  300.  * ========================================================================== */
  301. void Window_Activate(
  302.     WindowPtr            inWindow)
  303. {
  304.     WindowMethodPtr        activateMethod;
  305.     
  306.     if (inWindow != NULL)
  307.     {
  308.         activateMethod = Window_GetMethod(inWindow, kWindowMethod_Activate);
  309.         if (activateMethod != NULL)
  310.         {
  311.             (*activateMethod)(inWindow);
  312.         }
  313.     }
  314. }
  315.  
  316.  
  317. /* =============================================================================
  318.  *        Window_Deactivate (external)
  319.  *
  320.  *    Handles window deactivation.
  321.  * ========================================================================== */
  322. void Window_Deactivate(
  323.     WindowPtr            inWindow)
  324. {
  325.     WindowMethodPtr        deactivateMethod;
  326.     
  327.     if (inWindow != NULL)
  328.     {
  329.         deactivateMethod = Window_GetMethod(inWindow, kWindowMethod_Deactivate);
  330.         if (deactivateMethod != NULL)
  331.         {
  332.             (*deactivateMethod)(inWindow);
  333.         }
  334.     }
  335. }
  336.  
  337.  
  338.